package com.siecom.transport.serialport;

import android.os.SystemClock;
import android.util.Log;

import com.google.protobuf.InvalidProtocolBufferException;
import com.siecom.nativelibs.SerialPort;
import com.siecom.proto.Message;
import com.siecom.transport.CommServer;
import com.siecom.transport.Distribution;
import com.siecom.transport.handler.ServiceHandler;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by zhq on 2017/7/17.
 */

public class SerialServer extends CommServer {
    public final static String TAG = "SerialServer";
    protected SerialPort serialPort;
    private InputStream mInputStream;
    private OutputStream mOutputStream;
    private HashMap<Integer, ServiceHandler> handlerMap = Distribution.handlerMap;
    private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);

    public boolean initSerialPort(String serialDev, int baudRate) {

        try {
            serialPort = new SerialPort(new File(serialDev), baudRate, 0);
            mInputStream = serialPort.getInputStream();
            mOutputStream = serialPort.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();

            return false;
        }
        return true;
    }


    public void listenFromSerial() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
               synchronized (this) {
                   isInterrupted = false;

                   while (!isInterrupted) {

                       try {
//                        Log.e("listenFromSerial","----"+mInputStream.available());
                           Message.siecomproto msg = Message.siecomproto.parseDelimitedFrom(mInputStream);
                           int module = msg.getModule();
                           Log.e("module:", "serial ==" + module);
                           if (handlerMap.containsKey(module)) {
                               ServiceHandler handler = handlerMap.get(module);
                               handler.onReceive(SerialServer.this, msg,"");
                           }
                       } catch (IOException e) {
                           e.printStackTrace();

                           SystemClock.sleep(3);
                           break;
                       }
                   }
                   try {
                       mInputStream.close();
                   } catch (IOException e) {
                       e.printStackTrace();

                   }
               }

            }
        };
        fixedThreadPool.submit(runnable);
    }

    @Override
    public void close() {
        synchronized (this) {
            isInterrupted = true;
            try {
                if (serialPort != null) {
                    serialPort.close();
                    serialPort = null;
                }
                if (mInputStream != null)
                    mInputStream.close();
                mInputStream = null;
                if (mOutputStream != null)
                    mOutputStream.close();
                mOutputStream = null;

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendToRemote(Message.siecomproto msg) {
        synchronized (this) {
            try {
                mOutputStream = serialPort.getOutputStream();
                msg.writeDelimitedTo(mOutputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
